package mobidever.mygodutch.controls;

import java.util.ArrayList;
import java.util.List;

import mobidever.mygodutch.R;
import mobidever.mygodutch.adapter.AdapterSliderMenu;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class SliderMenu
{
	private Activity mActivity;
	private List mList;
	private ListView mListView ;
	private Boolean mIsClosed;
	private RelativeLayout layBottomBox;
	private AdapterSliderMenu mAdapterSliderMenu;
	private OnSliderMenuClickListener mSliderMenuClickListener;
	
	public interface OnSliderMenuClickListener
	{
		public void onSliderMenuClick(View pView,SliderItem pSliderItem);
	}
	
	public SliderMenu(Activity pActivity)
	{
		mActivity = pActivity;
		if (pActivity instanceof OnSliderMenuClickListener)
		{
			mSliderMenuClickListener = (OnSliderMenuClickListener) pActivity;
		}else {
			mSliderMenuClickListener = null;
		}
		
		initVariable();
		initView();
		initListener();
	}
	
	private void initVariable()
	{
		mList = new ArrayList();
		mIsClosed = true;
	}
	
	private void initView()
	{
		layBottomBox = (RelativeLayout) mActivity.findViewById(R.id.IncludeBottom);
	}
	
	public void bindList()
	{
		mAdapterSliderMenu = new AdapterSliderMenu(mList, mActivity);
		mListView = (ListView) mActivity.findViewById(R.id.lvBottomListViewBox);
		mListView.setAdapter(mAdapterSliderMenu);
		mListView.setOnItemClickListener(new OnSlideMenuItemClick());
	}
	
	private void initListener()
	{
		layBottomBox.setOnClickListener(new OnSliderMenuClick());
		
	}
	
	
	public void open()
	{
		RelativeLayout.LayoutParams _LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
		_LayoutParams.addRule(RelativeLayout.BELOW, R.id.IncludeTittle);
		layBottomBox.setLayoutParams(_LayoutParams);
		
		mIsClosed = false;
	}
	
	public void close()
	{
		RelativeLayout.LayoutParams _LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,100);
		_LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		layBottomBox.setLayoutParams(_LayoutParams);
		
		mIsClosed = true;
	}
	
	public void toggle()
	{
		if (mIsClosed)
		{
			open();
		}else {
			close();
		}
	}
	
	public void add(SliderItem pSilderItem)
	{
		mList.add(pSilderItem);
	}
	
	public void removeBottomBox()
	{
		RelativeLayout _MainLayout = (RelativeLayout)mActivity.findViewById(R.id.layMainLayout);
		_MainLayout.removeView(layBottomBox);
		layBottomBox = null;
	}
	
	private class OnSliderMenuClick implements OnClickListener
	{

		public void onClick(View v)
		{
			Log.i("Test", "OK");
			toggle();
		}
		
	}
	
	private class OnSlideMenuItemClick implements OnItemClickListener
	{

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			SliderItem _Item = (SliderItem) parent.getItemAtPosition(position);
			mSliderMenuClickListener.onSliderMenuClick(view, _Item);
		}
		
	}
}
